var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var common = require("common");
var urlUser = require("url/user");
var http = require("helper/http");
var nf = sm("do_Notification");
var root = ui("$");
var agreementFlag = true;
var preData = page.getData();
var thisTitle = "使用短信验证码登录";
if(preData&&preData.title&&preData.title!=="undefined"&&preData.title!==""){
	thisTitle = preData.title;
}
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : thisTitle
})
header.refreshData();

var loginBtn = ui("loginBtn");
var loginForm = ui("loginForm");

var addphone = loginForm.add("phone", "source://view/reuse/textField.ui",0,0);
var phone = ui("phone");
phone.bindData(hashdata);
hashdata.addData({
    "textFieldHint" : "输入手机号",
    "textFieldInputType" : "PHONENUMBER"
});
phone.refreshData();

var addBtnLogin = loginBtn.add("btnLogin", "source://view/reuse/button.ui",0,0);
var btnLogin = ui("btnLogin");
btnLogin.bindData(hashdata);
hashdata.addData({
    "btnText" : "下一步"
});
btnLogin.refreshData();

btnLogin.on("touch", function(data,e){
	page.hideKeyboard();//隐藏键盘
	var phoneValue = ui(addphone+".textField").text;
	if(!agreementFlag){
		nf.toast("请阅读并同意协议");
		return;
	}
	if(phoneValue.trim()===""){
		nf.toast("请输入手机号码");
		return;
	}
	http.post(urlUser.url.verifycode.addLoginPhoneCode,{"phone":phoneValue}, function(data) {
		nf.toast(data.userVerifycodeTips);
		common.open("source://view/user/loginCodeNext.ui",{"phone":phoneValue});
	});
});

var btnLoginPwd = ui("btnLoginPwd");
btnLoginPwd.on("touch", function(data,e){
	app.closePage();
	common.open("source://view/user/login.ui");
});
page.on("result", function(data) {
	if(data!=null&&data.isLogin==="1"){
		app.closePage(data);
	}
});
//圈圈生活协议
var agreementCheck = ui("agreementCheck");
var agreementCheckImg = ui("agreementCheckImg");
var agreeImg = ["source://image/common/checkboxcheck.png","source://image/common/checkbox.png"];
agreementCheck.on("checkChanged", function(data, e) {
	agreementFlag = this.checked;
	agreementCheckImg.source = agreementFlag?agreeImg[0]:agreeImg[1];
});
var agreementCheckLayout = ui("agreementCheckLayout");
agreementCheckLayout.on("touch", "", 300,function(data,e){
	common.open("source://view/user/agreement.ui");
});
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})